using MarriageFertility.Actions;
using MarriageFertility.Extensions;
using MarriageFertility.Helpers;
using MarriageFertility.Mcm;
using MarriageFertility.Notifications;
using MarriageFertility.Texts;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.Conversation;
using TaleWorlds.CampaignSystem.Conversation.Persuasion;
using TaleWorlds.Core;
using TaleWorlds.Localization;
using System.Collections.Generic;
using System.Linq;
using Helpers;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.CampaignSystem.Encounters;

namespace MarriageFertility.Dialogs;

public static class SexFriendDialog
{
    private static List<PersuasionTask> _allReservations;
    private static float _maximumScoreCap;

    private const PersuasionDifficulty _difficulty = PersuasionDifficulty.Hard;
    private const float _successValue = 0.8f;
    private const float _criticalSuccessValue = 1.5f;
    private const float _criticalFailValue = 3f;
    private const float _failValue = 0.2f;

    private enum SexFriendReservationType
    {
        Trust,       
        Attraction,   
        Discretion    
    }

    public static void AddDialogs(CampaignGameStarter campaignGameStarter)
    {
        AddSexFriendPersuasionDialogs(campaignGameStarter);
        AddIntimateNightDialogs(campaignGameStarter);
        AddCheatDialog(campaignGameStarter);
    }

    private static void AddSexFriendPersuasionDialogs(CampaignGameStarter campaignGameStarter)
    {
        campaignGameStarter.AddPlayerLine("sex_friend_flirt", "hero_main_options", "sex_friend_persuasion_start",
            "{=!}{FLIRTATION_LINE}", CanOpenSexFriendPersuasion, StartSexFriendPersuasion, 100);
        
        campaignGameStarter.AddDialogLine("sex_friend_persuasion_start", "sex_friend_persuasion_start", "sex_friend_next_reservation",
            SexFriendText.SexFriendStartResponse.ToString(), null, null, 100);
        
        campaignGameStarter.AddDialogLine("sex_friend_persuasion_fail", "sex_friend_next_reservation", "hero_main_options",
            "{=!}{FAILED_PERSUASION_LINE}", HasFailedPersuasion, EndPersuasion, 100, null);
        
        campaignGameStarter.AddDialogLine("sex_friend_persuasion_attempt", "sex_friend_next_reservation", "sex_friend_persuasion",
            "{=!}{PERSUASION_TASK_LINE}[if:convo_thinking]", CheckIfUnmetReservation, null, 100);
        
        campaignGameStarter.AddDialogLine("sex_friend_persuasion_success", "sex_friend_next_reservation", "close_window",
            "{=!}{SUCCESS_RESPONSE}", CheckIfPersuasionSuccessful, BecomeSexFriend, 100, null);
        
        AddPersuasionOption(campaignGameStarter, "sex_friend_argument_1", "sex_friend_persuasion", "sex_friend_reaction",
            "{=!}{SEX_FRIEND_PERSUADE_ATTEMPT_1}", CheckPersuasionOption1, ApplyPersuasionOption1, SetupPersuasionOption1);
        AddPersuasionOption(campaignGameStarter, "sex_friend_argument_2", "sex_friend_persuasion", "sex_friend_reaction",
            "{=!}{SEX_FRIEND_PERSUADE_ATTEMPT_2}", CheckPersuasionOption2, ApplyPersuasionOption2, SetupPersuasionOption2);
        AddPersuasionOption(campaignGameStarter, "sex_friend_argument_3", "sex_friend_persuasion", "sex_friend_reaction",
            "{=!}{SEX_FRIEND_PERSUADE_ATTEMPT_3}", CheckPersuasionOption3, ApplyPersuasionOption3, SetupPersuasionOption3);
        AddPersuasionOption(campaignGameStarter, "sex_friend_argument_4", "sex_friend_persuasion", "sex_friend_reaction",
            "{=!}{SEX_FRIEND_PERSUADE_ATTEMPT_4}", CheckPersuasionOption4, ApplyPersuasionOption4, SetupPersuasionOption4);
        
        campaignGameStarter.AddPlayerLine("sex_friend_no_answer", "sex_friend_persuasion", "sex_friend_no_answer_response",
            "{=0eAtiZbL}I have no answer to that.", null, null, 100);
        
        campaignGameStarter.AddDialogLine("sex_friend_no_answer_response", "sex_friend_no_answer_response", "hero_main_options",
            SexFriendText.NoAnswerResponse.ToString(), null, EndPersuasion, 100);
        
        campaignGameStarter.AddDialogLine("sex_friend_reaction", "sex_friend_reaction", "sex_friend_next_reservation",
            "{=!}{PERSUASION_REACTION}", CheckPersuasionReaction, ApplyPersuasionReaction, 100);
    }

    private static void AddPersuasionOption(CampaignGameStarter starter, string id, string inputToken, string outputToken,
        string text, ConversationSentence.OnConditionDelegate condition, ConversationSentence.OnConsequenceDelegate consequence,
        ConversationSentence.OnPersuasionOptionDelegate persuasionOption)
    {
        starter.AddPlayerLine(id, inputToken, outputToken, text, condition, consequence, 100,
            (out TextObject hintText) => CheckPersuasionOptionClickable(condition, out hintText), persuasionOption);
    }

    private static bool CheckPersuasionOptionClickable(ConversationSentence.OnConditionDelegate condition, out TextObject hintText)
    {
        hintText = TextObject.Empty;
        if (condition() && GetCurrentPersuasionTask().Options.Any(o => !o.IsBlocked))
            return true;
        hintText = new TextObject("{=9ACJsI6S}Blocked");
        return false;
    }

    private static void AddIntimateNightDialogs(CampaignGameStarter campaignGameStarter)
    {
        campaignGameStarter.AddPlayerLine("invitation_for_intimate_night", "hero_main_options",
            "invitation_for_intimate_night_request", SexFriendText.InvitationForIntimateNightText.ToString(),
            CanIntimateNight, null);

        campaignGameStarter.AddDialogLine("willingness_to_show_skills", "invitation_for_intimate_night_request",
            "hero_leave", SexFriendText.WillingnessToShowSkillsText.ToString(), null, IntimateNight);
    }

    private static void AddCheatDialog(CampaignGameStarter campaignGameStarter)
    {
        campaignGameStarter.AddPlayerLine("become_cheatRelation_request", "hero_main_options",
            "become_cheatRelation_response",
            SexFriendText.CheatRelationRequestText.ToString(), EnableCheat, null);

        campaignGameStarter.AddDialogLine("become_cheatRelation_response", "become_cheatRelation_response",
            "hero_main_options",
            SexFriendText.CheatRelationResponseText.ToString(), null, ChangeRelation);
    }

    private static void ChangeRelation()
    {
        Hero hero = Hero.OneToOneConversationHero;
        if (hero != null)
        {
            ChangeRelationAction.ApplyPlayerRelation(hero, 100);
        }
    }
    
    private static bool CanOpenSexFriendPersuasion()
    {
        Hero hero = Hero.OneToOneConversationHero;
        if (hero == null || !MCMSettings.Instance.EnableSexFriendDialog || !hero.IsAlive ||
            hero.IsFemale == Hero.MainHero.IsFemale || hero.IsSexFriend(Hero.MainHero) || hero.IsPlayerSpouse())
            return false;
        
        if (hero.GetRelationWithPlayer() < 0) return false;

        MBTextManager.SetTextVariable("FLIRTATION_LINE", Hero.MainHero.IsFemale
            ? SexFriendText.FlirtFemale.ToString()
            : SexFriendText.FlirtMale.ToString());

        return true;
    }

    private static void StartSexFriendPersuasion()
    {
        Hero hero = Hero.OneToOneConversationHero;
        if (hero != null && !HeroExtension.SexFriendPersuasionStarted.ContainsKey(hero))
        {
            HeroExtension.SexFriendPersuasionStarted[hero] = true;
            _allReservations = GetPersuasionTasksForSexFriend(hero, Hero.MainHero);
            _maximumScoreCap = _allReservations.Count * 1f;
            float initialProgress = CalculateInitialProgress();
            ConversationManager.StartPersuasion(_maximumScoreCap, _successValue, _failValue, _criticalSuccessValue, _criticalFailValue, initialProgress, _difficulty);
        }
    }

    private static bool CanIntimateNight()
    {
        Hero hero = Hero.OneToOneConversationHero;
        return hero.IsSexFriend(Hero.MainHero) || hero.IsPlayerSpouse();
    }

    private static bool EnableCheat()
    {
        return MCMSettings.Instance.RelationCheat;
    }

    private static float CalculateInitialProgress()
    {
        float progress = 0f;
        foreach (var task in _allReservations)
        {
            foreach (var attempt in HeroExtension.PreviousSexFriendPersuasionAttempts)
            {
                if (attempt.Matches(Hero.OneToOneConversationHero, task.ReservationType))
                {
                    switch (attempt.Result)
                    {
                        case PersuasionOptionResult.CriticalFailure: progress -= _criticalFailValue; break;
                        case PersuasionOptionResult.Failure: progress -= _failValue; break;
                        case PersuasionOptionResult.Success: progress += _successValue; break;
                        case PersuasionOptionResult.CriticalSuccess: progress += _criticalSuccessValue; break;
                    }
                }
            }
        }
        RemoveOldAttempts();
        return progress;
    }

    private static void RemoveOldAttempts()
    {
        HeroExtension.PreviousSexFriendPersuasionAttempts.RemoveAll(a => a.GameTime.ElapsedYearsUntilNow > 1f || !a.PersuadedHero.IsAlive);
    }

    private static bool CanAttemptToPersuade(Hero targetHero, int reservationType)
    {
        return !HeroExtension.PreviousSexFriendPersuasionAttempts.Any(a => a.Matches(targetHero, reservationType) && !a.IsSuccesful() && a.GameTime.ElapsedWeeksUntilNow < 1f);
    }

    private static void BecomeSexFriend()
    {
        Hero hero = Hero.OneToOneConversationHero;
        if (hero != null)
        {
            Hero.MainHero.BecomeSexFriend(hero);
            hero.SetHasMet();
            ShowMessage(Hero.MainHero, hero);
            HeroExtension.SexFriendPersuasionStarted.Remove(hero);
        }
        if (PlayerEncounter.Current != null)
            PlayerEncounter.LeaveEncounter = true;
        _allReservations = null;
        ConversationManager.EndPersuasion();
    }

    private static void IntimateNight()
    {
        Hero hero = Hero.OneToOneConversationHero;
        if (hero != null)
        {
            hero.TryPregnancy();
            ChangeRelationAction.ApplyPlayerRelation(hero, 10);
            Log.CommonMessage(SexFriendText.GentleIntimacyDescriptionText, TextColor.Pink);
            if (RelationHelper.HotButterFound)
            {
                MBInformationManager.ShowSceneNotification(new HotButterNotification(Hero.MainHero, hero, hero.CurrentSettlement, null));
            }
        }
    }

    private static void ShowMessage(Hero firstHero, Hero secondHero)
    {
        TextObject message = MessageText.BecomeSexFriendMessage;
        message.SetTextVariable("firstHero", firstHero.Name);
        message.SetTextVariable("secondHero", secondHero.Name);
        MBInformationManager.AddQuickInformation(message);
    }

    private static void EndPersuasion()
    {
        Hero hero = Hero.OneToOneConversationHero;
        if (hero != null)
        {
            HeroExtension.SexFriendPersuasionStarted.Remove(hero);
        }
        _allReservations = null;
        ConversationManager.EndPersuasion();
        if (PlayerEncounter.Current != null)
            PlayerEncounter.LeaveEncounter = true;
    }

    private static bool HasFailedPersuasion()
    {
        var task = GetCurrentPersuasionTask();
        if(task?.Options==null || !task.Options.Any()) return false;
        if (task.Options.All(o => o.IsBlocked) && !ConversationManager.GetPersuasionProgressSatisfied())
        {
            SelectRandomRejectResponse();
            return true;
        }
        return false;
    }

    private static bool CheckIfUnmetReservation()
    {
        var task = GetCurrentPersuasionTask();
        if (task == _allReservations.Last() && task.Options.All(o => o.IsBlocked) || ConversationManager.GetPersuasionProgressSatisfied())
            return false;
        MBTextManager.SetTextVariable("PERSUASION_TASK_LINE", task.SpokenLine);
        return true;
    }

    private static bool CheckIfPersuasionSuccessful()
    {
        bool isSuccess= ConversationManager.GetPersuasionProgressSatisfied();
        if (isSuccess)
        {
            SelectRandomSuccessResponse();
        }
        return isSuccess;
    }

    private static void SelectRandomSuccessResponse()
    {
        var successResponses = new List<TextObject>
        {
            SexFriendText.AcceptResponse1, SexFriendText.AcceptResponse2, SexFriendText.AcceptResponse3,
            SexFriendText.AcceptResponse4, SexFriendText.AcceptResponse5, SexFriendText.AcceptResponse6,
            SexFriendText.AcceptResponse7, SexFriendText.AcceptResponse8, SexFriendText.AcceptResponse9,
            SexFriendText.AcceptResponse10, SexFriendText.AcceptResponse11, SexFriendText.AcceptResponse12,
            SexFriendText.AcceptResponse13, SexFriendText.AcceptResponse14, SexFriendText.AcceptResponse15,
            SexFriendText.AcceptResponse16, SexFriendText.AcceptResponse17, SexFriendText.AcceptResponse18,
            SexFriendText.AcceptResponse19, SexFriendText.AcceptResponse20, SexFriendText.AcceptResponse21,
            SexFriendText.AcceptResponse22, SexFriendText.AcceptResponse23, SexFriendText.AcceptResponse24,
            SexFriendText.AcceptResponse25
        };
        MBTextManager.SetTextVariable("SUCCESS_RESPONSE", successResponses[MBRandom.RandomInt(successResponses.Count)]);
    }

    private static void SelectRandomRejectResponse()
    {
        var rejectResponses = new List<TextObject>
        {
            SexFriendText.RejectResponse1, SexFriendText.RejectResponse2, SexFriendText.RejectResponse3,
            SexFriendText.RejectResponse4, SexFriendText.RejectResponse5, SexFriendText.RejectResponse6,
            SexFriendText.RejectResponse7, SexFriendText.RejectResponse8, SexFriendText.RejectResponse9
        };
        MBTextManager.SetTextVariable("FAILED_PERSUASION_LINE", rejectResponses[MBRandom.RandomInt(rejectResponses.Count)]);
    }

    private static PersuasionTask GetCurrentPersuasionTask()
    {
        foreach (var task in _allReservations)
        {
            if (!task.Options.All(o => o.IsBlocked))
                return task;
        }
        return _allReservations.Last();
    }

   private static List<PersuasionTask> GetPersuasionTasksForSexFriend(Hero target, Hero initiator)
    {
        var tasks = new List<PersuasionTask>();
        StringHelpers.SetCharacterProperties("PLAYER", initiator.CharacterObject);
        float relationship = target.GetRelationWithPlayer();
        
        if (CanAttemptToPersuade(target, (int)SexFriendReservationType.Trust))
        {
            var trustTask = new PersuasionTask((int)SexFriendReservationType.Trust)
            {
                SpokenLine = SexFriendText.TrustSpoken,
                ImmediateFailLine = SexFriendText.TrustImmediateFail,
                FinalFailLine = SexFriendText.TrustFail,
                TryLaterLine = SexFriendText.TrustTryLater 
            };

            PersuasionArgumentStrength trustStrength;
            if (relationship <= -10)
            {
                trustTask.SpokenLine = SexFriendText.TrustSpokenLowRelation;
                trustStrength = PersuasionArgumentStrength.ExtremelyHard;
            }
            else if (relationship <= 0)
            {
                trustTask.SpokenLine = SexFriendText.TrustSpokenNeutral;
                trustStrength = PersuasionArgumentStrength.VeryHard;
            }
            else if (relationship >= 75)
            {
                trustTask.SpokenLine = SexFriendText.TrustSpokenHighRelation;
                trustStrength = PersuasionArgumentStrength.Easy;
            }
            else if (relationship >= 50)
            {
                trustTask.SpokenLine = SexFriendText.TrustSpokenHighRelation;
                trustStrength = PersuasionArgumentStrength.Normal;
            }
            else
            {
                trustStrength = PersuasionArgumentStrength.Hard;
            }

            if (relationship >= 75)
            {
                trustTask.AddOptionToTask(new PersuasionOptionArgs(
                    DefaultSkills.Charm, DefaultTraits.Honor, TraitEffect.Positive, trustStrength, true,
                    SexFriendText.Negotiation1, null, false, true, false)); // "想试试我的大香肠吗"
            }
            else if (target.GetTraitLevel(DefaultTraits.Honor) > 0)
            {
                trustTask.AddOptionToTask(new PersuasionOptionArgs(
                    DefaultSkills.Charm, DefaultTraits.Honor, TraitEffect.Positive, trustStrength, true,
                    SexFriendText.Negotiation2, null, false, true, false)); // "我能让你有更爽的打炮体验"
            }
            trustTask.AddOptionToTask(new PersuasionOptionArgs(
                DefaultSkills.Charm, DefaultTraits.Calculating, TraitEffect.Positive, trustStrength + 1, false,
                SexFriendText.Negotiation3, null, false, true, false)); // "我的宝剑需要一把剑鞘"
            trustTask.AddOptionToTask(new PersuasionOptionArgs(
                DefaultSkills.Roguery, DefaultTraits.Calculating, TraitEffect.Positive, trustStrength + 2, false,
                SexFriendText.Negotiation4, null, false, true, false)); // "准备好忘记烦恼了吗，宝贝"
            tasks.Add(trustTask);
        }

        // Task 2: Attraction (Is the NPC attracted to the player?)
        if (CanAttemptToPersuade(target, (int)SexFriendReservationType.Attraction))
        {
            var attractionTask = new PersuasionTask((int)SexFriendReservationType.Attraction)
            {
                SpokenLine = SexFriendText.AttractionSpoken,
                ImmediateFailLine = SexFriendText.AttractionImmediateFail,
                FinalFailLine = SexFriendText.AttractionFail,
                TryLaterLine = SexFriendText.AttractionTryLater // 添加TryLaterLine
            };

            PersuasionArgumentStrength attractionStrength;
            if (relationship >= 80 || (MBRandom.RandomFloat > 0.95f && target.GetTraitLevel(DefaultTraits.Valor) > 0))
            {
                attractionTask.SpokenLine = SexFriendText.AttractionSpokenHigh;
                attractionStrength = PersuasionArgumentStrength.VeryEasy;
            }
            else if (relationship >= 60)
            {
                attractionTask.SpokenLine = SexFriendText.AttractionSpokenHigh;
                attractionStrength = PersuasionArgumentStrength.Normal;
            }
            else if (target.GetTraitLevel(DefaultTraits.Valor) > 0)
            {
                attractionTask.SpokenLine = SexFriendText.AttractionSpokenValor;
                attractionStrength = PersuasionArgumentStrength.Hard;
            }
            else
            {
                attractionStrength = PersuasionArgumentStrength.VeryHard;
            }

            if (target.GetTraitLevel(DefaultTraits.Valor) > 0)
            {
                attractionTask.AddOptionToTask(new PersuasionOptionArgs(
                    DefaultSkills.Charm, DefaultTraits.Valor, TraitEffect.Positive, attractionStrength, true,
                    SexFriendText.Negotiation5, null, false, true, false)); // "我们可以有更加深入的交流"
            }
            attractionTask.AddOptionToTask(new PersuasionOptionArgs(
                DefaultSkills.Charm, DefaultTraits.Mercy, TraitEffect.Positive, attractionStrength, false,
                SexFriendText.Negotiation6, null, false, true, false)); // "我相信你现在已经湿得一塌糊涂了"
            attractionTask.AddOptionToTask(new PersuasionOptionArgs(
                DefaultSkills.Roguery, DefaultTraits.Calculating, TraitEffect.Positive, attractionStrength + 1, false,
                SexFriendText.Negotiation7, null, false, true, false)); // "快来吸我的大几把"
            attractionTask.AddOptionToTask(new PersuasionOptionArgs(
                DefaultSkills.Charm, DefaultTraits.Calculating, TraitEffect.Positive, attractionStrength + 2, false,
                SexFriendText.Negotiation8, null, false, true, false)); // "我能让你的三个洞都满足"
            tasks.Add(attractionTask);
        }

        // Task 3: Discretion (Can the relationship remain secret?)
        if (CanAttemptToPersuade(target, (int)SexFriendReservationType.Discretion))
        {
            var discretionTask = new PersuasionTask((int)SexFriendReservationType.Discretion)
            {
                SpokenLine = SexFriendText.DiscretionSpoken,
                ImmediateFailLine = SexFriendText.DiscretionImmediateFail,
                FinalFailLine = SexFriendText.DiscretionFail,
                TryLaterLine = SexFriendText.DiscretionTryLater // 添加TryLaterLine
            };

            PersuasionArgumentStrength discretionStrength;
            if (target.GetTraitLevel(DefaultTraits.Honor) > 0)
            {
                discretionTask.SpokenLine = SexFriendText.DiscretionSpokenHonor;
                discretionStrength = PersuasionArgumentStrength.ExtremelyHard;
            }
            else if (relationship >= 75 && target.GetTraitLevel(DefaultTraits.Honor) <= 0)
            {
                discretionTask.SpokenLine = SexFriendText.DiscretionSpokenFriend;
                discretionStrength = PersuasionArgumentStrength.Easy;
            }
            else if (relationship >= 50)
            {
                discretionTask.SpokenLine = SexFriendText.DiscretionSpokenFriend;
                discretionStrength = PersuasionArgumentStrength.Normal;
            }
            else
            {
                discretionStrength = PersuasionArgumentStrength.Hard;
            }

            discretionTask.AddOptionToTask(new PersuasionOptionArgs(
                DefaultSkills.Charm, DefaultTraits.Honor, TraitEffect.Positive, discretionStrength, true,
                SexFriendText.Negotiation9, null, false, true, false)); // "你该试试更多的性体验"
            discretionTask.AddOptionToTask(new PersuasionOptionArgs(
                DefaultSkills.Roguery, DefaultTraits.Calculating, TraitEffect.Positive, discretionStrength, false,
                SexFriendText.Negotiation10, null, false, true, false)); // "你肯定没试过我这么粗的几把"
            discretionTask.AddOptionToTask(new PersuasionOptionArgs(
                DefaultSkills.Charm, DefaultTraits.Calculating, TraitEffect.Positive, discretionStrength + 1, false,
                SexFriendText.Negotiation11, null, false, true, false)); // "我能和你干一整天"
            discretionTask.AddOptionToTask(new PersuasionOptionArgs(
                DefaultSkills.Roguery, DefaultTraits.Honor, TraitEffect.Positive, discretionStrength + 2, false,
                SexFriendText.Negotiation12, null, false, true, false)); // "光看到你，我的几把就硬得可怕"
            tasks.Add(discretionTask);
        }

        return tasks;
    }
    private static bool CheckPersuasionOption1()
    {
        var task = GetCurrentPersuasionTask();
        if (task.Options.Count > 0)
        {
            var text = new TextObject("{=bSo9hKwr}{PERSUASION_OPTION_LINE} {SUCCESS_CHANCE}");
            text.SetTextVariable("SUCCESS_CHANCE", PersuasionHelper.ShowSuccess(task.Options[0]));
            text.SetTextVariable("PERSUASION_OPTION_LINE", task.Options[0].Line);
            MBTextManager.SetTextVariable("SEX_FRIEND_PERSUADE_ATTEMPT_1", text);
            return true;
        }
        return false;
    }

    private static bool CheckPersuasionOption2()
    {
        var task = GetCurrentPersuasionTask();
        if (task.Options.Count > 1)
        {
            var text = new TextObject("{=bSo9hKwr}{PERSUASION_OPTION_LINE} {SUCCESS_CHANCE}");
            text.SetTextVariable("SUCCESS_CHANCE", PersuasionHelper.ShowSuccess(task.Options[1]));
            text.SetTextVariable("PERSUASION_OPTION_LINE", task.Options[1].Line);
            MBTextManager.SetTextVariable("SEX_FRIEND_PERSUADE_ATTEMPT_2", text);
            return true;
        }
        return false;
    }

    private static bool CheckPersuasionOption3()
    {
        var task = GetCurrentPersuasionTask();
        if (task.Options.Count > 2)
        {
            var text = new TextObject("{=bSo9hKwr}{PERSUASION_OPTION_LINE} {SUCCESS_CHANCE}");
            text.SetTextVariable("SUCCESS_CHANCE", PersuasionHelper.ShowSuccess(task.Options[2]));
            text.SetTextVariable("PERSUASION_OPTION_LINE", task.Options[2].Line);
            MBTextManager.SetTextVariable("SEX_FRIEND_PERSUADE_ATTEMPT_3", text);
            return true;
        }
        return false;
    }

    private static bool CheckPersuasionOption4()
    {
        var task = GetCurrentPersuasionTask();
        if (task.Options.Count > 3)
        {
            var text = new TextObject("{=bSo9hKwr}{PERSUASION_OPTION_LINE} {SUCCESS_CHANCE}");
            text.SetTextVariable("SUCCESS_CHANCE", PersuasionHelper.ShowSuccess(task.Options[3]));
            text.SetTextVariable("PERSUASION_OPTION_LINE", task.Options[3].Line);
            MBTextManager.SetTextVariable("SEX_FRIEND_PERSUADE_ATTEMPT_4", text);
            return true;
        }
        return false;
    }

    private static void ApplyPersuasionOption1()
    {
        var task = GetCurrentPersuasionTask();
        if (task.Options.Count > 0)
            task.Options[0].BlockTheOption(true);
    }

    private static void ApplyPersuasionOption2()
    {
        var task = GetCurrentPersuasionTask();
        if (task.Options.Count > 1)
            task.Options[1].BlockTheOption(true);
    }

    private static void ApplyPersuasionOption3()
    {
        var task = GetCurrentPersuasionTask();
        if (task.Options.Count > 2)
            task.Options[2].BlockTheOption(true);
    }

    private static void ApplyPersuasionOption4()
    {
        var task = GetCurrentPersuasionTask();
        if (task.Options.Count > 3)
            task.Options[3].BlockTheOption(true);
    }

    private static PersuasionOptionArgs SetupPersuasionOption1() => GetCurrentPersuasionTask().Options.ElementAt(0);
    private static PersuasionOptionArgs SetupPersuasionOption2() => GetCurrentPersuasionTask().Options.ElementAt(1);
    private static PersuasionOptionArgs SetupPersuasionOption3() => GetCurrentPersuasionTask().Options.ElementAt(2);
    private static PersuasionOptionArgs SetupPersuasionOption4() => GetCurrentPersuasionTask().Options.ElementAt(3);

    private static bool CheckPersuasionReaction()
    {
        var result = ConversationManager.GetPersuasionChosenOptions().Last().Item2;
        var task = GetCurrentPersuasionTask();
        switch (result)
        {
            case PersuasionOptionResult.CriticalFailure:
                MBTextManager.SetTextVariable("PERSUASION_REACTION", task.ImmediateFailLine ?? SexFriendText.Stage2ReactionCriticalFailure);
                _allReservations.ForEach(t => t.BlockAllOptions());
                break;
            case PersuasionOptionResult.Failure:
                MBTextManager.SetTextVariable("PERSUASION_REACTION", task.ImmediateFailLine ?? SexFriendText.Stage2ReactionFailure);
                break;
            case PersuasionOptionResult.Success:
                MBTextManager.SetTextVariable("PERSUASION_REACTION", SexFriendText.Stage2ReactionSuccess);
                break;
            case PersuasionOptionResult.CriticalSuccess:
                MBTextManager.SetTextVariable("PERSUASION_REACTION", SexFriendText.Stage2ReactionCriticalSuccess);
                break;
            default:
                MBTextManager.SetTextVariable("PERSUASION_REACTION", PersuasionHelper.GetDefaultPersuasionOptionReaction(result));
                break;
        }
        return true;
    }

    private static void ApplyPersuasionReaction()
    {
        var task = GetCurrentPersuasionTask();
        var chosenOption = ConversationManager.GetPersuasionChosenOptions().Last();
        float difficulty = Campaign.Current.Models.PersuasionModel.GetDifficulty(_difficulty);
        float moveToNextStageChance, blockRandomOptionChance;
        Campaign.Current.Models.PersuasionModel.GetEffectChances(chosenOption.Item1, out moveToNextStageChance, out blockRandomOptionChance, difficulty);
        task.ApplyEffects(moveToNextStageChance, blockRandomOptionChance);
        HeroExtension.PreviousSexFriendPersuasionAttempts.Add(new PersuasionAttempt(Hero.OneToOneConversationHero, CampaignTime.Now, chosenOption.Item1, chosenOption.Item2, task.ReservationType));
    }
}